using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using MM.Entities;

namespace MM.Business
{
    class clsCHI_TIET_CD_TK
    {
        public clsCHI_TIET_CD_TK(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        public List<CHI_TIET_CD_TK> DataTableToLstObj(DataTable dt)
        {
            CHI_TIET_CD_TK chiTietCdTkDTO;
            List<CHI_TIET_CD_TK> lstchiTietCdTkDTO = new List<CHI_TIET_CD_TK>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                chiTietCdTkDTO = new CHI_TIET_CD_TK();
                try
                {
                    chiTietCdTkDTO._maChiTiet = int.Parse(dt.Rows[i]["MaChiTiet"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    chiTietCdTkDTO._maChucDanh = int.Parse(dt.Rows[i]["MaChucDanh"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    chiTietCdTkDTO._maTaiKhoan = int.Parse(dt.Rows[i]["MaTaiKhoan"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    chiTietCdTkDTO._ngayBatDau = DateTime.Parse(dt.Rows[i]["NgayBatDau"].ToString());
                }
                catch (Exception ex)
                {
                }
                try
                {
                    chiTietCdTkDTO._ngayKetThuc = DateTime.Parse(dt.Rows[i]["NgayKetThuc"].ToString());
                }
                catch (Exception ex)
                {
                }
                lstchiTietCdTkDTO.Add(chiTietCdTkDTO);
            }
            return lstchiTietCdTkDTO;
        }
        public CHI_TIET_CD_TK DataTableToObj(DataTable dt)
        {
            CHI_TIET_CD_TK chiTietCdTkDTO = new CHI_TIET_CD_TK();
            try
            {
                chiTietCdTkDTO._maChiTiet = int.Parse(dt.Rows[0]["MaChiTiet"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                chiTietCdTkDTO._maChucDanh = int.Parse(dt.Rows[0]["MaChucDanh"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                chiTietCdTkDTO._maTaiKhoan = int.Parse(dt.Rows[0]["MaTaiKhoan"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                chiTietCdTkDTO._ngayBatDau = DateTime.Parse(dt.Rows[0]["NgayBatDau"].ToString());
            }
            catch (Exception ex)
            {
            }
            try
            {
                chiTietCdTkDTO._ngayKetThuc = DateTime.Parse(dt.Rows[0]["NgayKetThuc"].ToString());
            }
            catch (Exception ex)
            {
            }
            return chiTietCdTkDTO;
        }
        //public List<CHUC_DANH> LoadChucDanh()
        //{
        //    DataTable dt = new DataTable();
        //    List<CHUC_DANH> lstCdDTO = new List<CHUC_DANH>();
        //    try
        //    {
        //        SqlHelper.OpenConnection();
        //        string spName = "usp_SelectCHUC_DANHsAll";
        //        dt = SqlHelper.ExecuteToDataTable(spName, CommandType.StoredProcedure);
        //        lstCdDTO = DataTableToLstObj(dt);
        //    }
        //    catch (Exception ex)
        //    {
        //    }
        //    finally
        //    {
        //        if (!SqlHelper.IsHaveTran())
        //            SqlHelper.CloseConnection();
        //    }
        //    return lstCdDTO;
        //}

        public int InsertCHI_TIET_CD_TK(int maTk, int maCd, DateTime ngayBd, DateTime ngayKt)
        {
            int result = 0;
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_InsertUpdateCHI_TIET_CD_TK";
                SqlParameter[] par = new SqlParameter[4];
                par[0] = new SqlParameter("@MaTaiKhoan", maTk);
                par[1] = new SqlParameter("@MaChucDanh", maCd);
                par[2] = new SqlParameter("@NgayBatDau", ngayBd);
                par[3] = new SqlParameter("@NgayKetThuc", ngayKt);
                result = SqlHelper.ExecuteNonQuery(spName, par);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return result;
        }

        public List<CHI_TIET_CD_TK> SelectCHI_TIET_CD_TKByMaCDVaMaTK(int maCd, int maTk)
        {
            List<CHI_TIET_CD_TK> lstCdTk = new List<CHI_TIET_CD_TK>();
            DataTable dt = new DataTable();
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_SelectCHI_TIET_CD_TKByMaCDVaMaTK";
                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@MaTaiKhoan", maTk);
                par[1] = new SqlParameter("@MaChucDanh", maCd);
                dt = SqlHelper.ExecuteToDataTable(spName, par);
                lstCdTk = DataTableToLstObj(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lstCdTk;
        }

        public int UpdateCHI_TIET_CD_TK(int maChiTiet, DateTime ngayBd, DateTime ngayKt, int tinhTrang)
        {
            int result = 0;
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_UpdateCHI_TIET_CD_TK";
                SqlParameter[] par = new SqlParameter[4];
                par[0] = new SqlParameter("@MaChiTiet", maChiTiet);
                par[1] = new SqlParameter("@TinhTrang", tinhTrang);
                par[2] = new SqlParameter("@NgayBatDau", ngayBd);
                par[3] = new SqlParameter("@NgayKetThuc", ngayKt);
                result = SqlHelper.ExecuteNonQuery(spName, par);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return result;
        }
    }
}
